package com.anthonybennis.blender.client.pages;

import com.anthonybennis.blender.client.Navigatgor;
import com.anthonybennis.blender.client.Navigatgor.PAGE;
import com.anthonybennis.blender.client.handlers.HomePageTileClickHandler;
import com.anthonybennis.blender.client.images.ImageClientBundle;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.ui.client.widget.button.Button;
import com.googlecode.mgwt.ui.client.widget.touch.TouchPanel;

/**
 * Home Page, with links to all the apps pages.
 * @author abennis
 */
public class HomePage 
{
	private final Navigatgor _navigator;
	private final ImageClientBundle _imageClientBundle;
	/**
	 * Constructor
	 */
	public HomePage(ImageClientBundle imageClientBundle, Navigatgor navigator)
	{
		_imageClientBundle = imageClientBundle;
		_navigator = navigator;
	}
	
	/**
	 * 
	 * @return
	 */
	public Widget create()
	{
		FlowPanel mainContainer = new FlowPanel();
		mainContainer.setStyleName("home-page-main-container-panel");
		mainContainer.getElement().getStyle().setBackgroundColor("WHITE");
		
		/*
		 * First Row
		 */
		mainContainer.setSize("90%","90%");
		mainContainer.add(this.createKeyboardShortsTile());
		
		/*
		 * 2nd row will exist in future, maybe.
		 */
		
		return mainContainer;
	}
	
	/**
	 * 
	 * @return
	 */
	private Widget createKeyboardShortsTile()
	{
		Widget tile = this.createTile(PAGE.SHORTCUTS, "shortcuts-tile", "Keyboard Short cuts", _imageClientBundle.shortcutsTileImage());
		return tile;
	}
	
	/**
	 * 
	 * @return
	 */
	private Widget createWikiTile()
	{
		
		Widget tile = this.createTile(PAGE.WIKI, "wiki-tile", "Wiki",_imageClientBundle.wikiTileImage());
		return tile;
	}

	
	/**
	 * 
	 * @return
	 */
	private Widget createOpenMoviesTile()
	{
		Widget tile = this.createTile(PAGE.VIDEOS, "open-movies-tile", "Open Movies",_imageClientBundle.openMovieTileImage());
		return tile;
	}
	
	/**
	 * 
	 * @return
	 */
	private Widget createBestWebsitesTile()
	{
		Widget tile = this.createTile(PAGE.NEWS, "best-websites-tile", "News",_imageClientBundle.bestWebSitesTileImage());
		return tile;
	}
	
	/**
	 * 
	 * @param styleName
	 * @param nackgroundColour
	 * @param widthPercentage
	 * @return
	 */
	private Widget createTile(PAGE type, String styleName, String name, ImageResource imageResource)
	{
		DockLayoutPanel tile = new DockLayoutPanel(Unit.PCT);
		
		tile.setStyleName(styleName);
		tile.getElement().getStyle().setFloat(Style.Float.LEFT);
		tile.getElement().getStyle().setMarginLeft(0.5, Unit.PCT);
		tile.getElement().getStyle().setMarginRight(.5, Unit.PCT);
		tile.getElement().getStyle().setMarginBottom(1, Unit.PCT);
		
		/*
		 * Flat Button
		 */
		Button labelButton = new Button(name);
		labelButton.setStyleName(styleName + "-label"); // Dynamically create CSS Style name for label.
		labelButton.setWidth("100%");
		labelButton.addTapHandler(new HomePageTileClickHandler(type, _navigator));
		tile.addSouth(labelButton, 23);
		
		/*
		 * Image Button
		 */
		Image tileImage = new Image(imageResource);
		PushButton imageButton = new PushButton(tileImage);
		imageButton.setSize(tileImage.getWidth() + "px", tileImage.getHeight() + "px");
		imageButton.setStyleName(styleName);
		Panel imageButtonContainer = this.createCenteredContainerPanel();
		imageButtonContainer.add(imageButton);
		
		/*
		 * Clickable Image container.
		 */
		TouchPanel clickablePanel = new TouchPanel();
		clickablePanel.addTapHandler(new HomePageTileClickHandler(type, _navigator));
		clickablePanel.add(imageButtonContainer);
		tile.add(clickablePanel);
		
		return tile;
	}
	
	/**
	 * TODO AB: Move to Utils.
	 */
	private Panel createCenteredContainerPanel()
	{
		VerticalPanel centeredContainerPanel = new VerticalPanel();
		centeredContainerPanel.setWidth("100%");
		centeredContainerPanel.setHeight("100%");
		centeredContainerPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		centeredContainerPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		return centeredContainerPanel;
	}
}